export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}
export function scorePassword (pass) {
  let score = 0
  if (!pass) {
    return score
  }
  // award every unique letter until 5 repetitions
  const letters = {}
  for (let i = 0; i < pass.length; i++) {
      letters[pass[i]] = (letters[pass[i]] || 0) + 1
      score += 5.0 / letters[pass[i]]
  }

  // bonus points for mixing it up
  const variations = {
      digits: /\d/.test(pass),
      lower: /[a-z]/.test(pass),
      upper: /[A-Z]/.test(pass),
      nonWords: /\W/.test(pass)
  }

  let variationCount = 0
  for (var check in variations) {
      variationCount += (variations[check] === true) ? 1 : 0
  }
  score += (variationCount - 1) * 10

  return parseInt(score)
}
export const IsDefine = value => {
  if (
    value == null ||
    value === '' ||
    value === 'undefined' ||
    value === undefined ||
    value === 'null' ||
    value === '(null)' ||
    value === 'NULL' ||
    typeof value === 'undefined'
  ) {
    return false;
  } else {
    value = value + '';
    value = value.replace(/\s/g, '');
    if (value === '') {
      return false;
    }
    return true;
  }
};
export function parseTime (d) {
  if (!IsDefine(d)) {
    return '';
  }
  if (typeof d === 'string') {
    return d;
  }
  var month = '';
    var date = '';
    var hours = '';
    var minutes = '';
    var seconds = '';
  if (d.getMonth() + 1 < 10) {
    month = '0' + (d.getMonth() + 1);
  } else {
    month = d.getMonth() + 1;
  }
  if (d.getDate() < 10) {
    date = '0' + d.getDate();
  } else {
    date = d.getDate();
  }
  if (d.getHours() < 10) {
    hours = '0' + d.getHours();
  } else {
    hours = d.getHours();
  }
  if (d.getMinutes() < 10) {
    minutes = '0' + d.getMinutes();
  } else {
    minutes = d.getMinutes();
  }
  if (d.getSeconds() < 10) {
    seconds = '0' + d.getSeconds();
  } else {
    seconds = d.getSeconds();
  }
  // const newDate = d.getFullYear() + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds;
  const newDate = d.getFullYear() + '-' + month + '-' + date + ' ' + hours + ':' + minutes;
  return newDate;
}
const format = (time) => {
  let ymd = ''
  const mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
  const day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
  ymd += time.getFullYear() + '-' // 获取年份。
  ymd += mouth + '-' // 获取月份。
  ymd += day // 获取日。
  return ymd // 返回日期。
 }
// 处理排班时间段
 export const getAllDate = (start, end) => {
  const dateArr = []
  const startArr = start.split('-')
  const endArr = end.split('-')
  const db = new Date()
  db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
  const de = new Date()
  de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
  const unixDb = db.getTime()
  const unixDe = de.getTime()
  let stamp
  const oneDay = 24 * 60 * 60 * 1000;
  for (stamp = unixDb; stamp <= unixDe;) {
    const obj = {
      plan_date: '',
      class_key: ''
    }
    obj.plan_date = format(new Date(parseInt(stamp)))
    dateArr.push(obj)
    stamp = stamp + oneDay
  }
  return dateArr
 }

  /**
     * 数组转树形结构
     * @param list 源数组
     * @param tree 树
     * @param parentId 父ID
     */
  export const listToTree = (list, parentId) => {
    let tree = []
    list.forEach((item) => {
      // 判断是否为父级菜单
      if (item.parent_menu_number == parentId) {
        // 迭代 list， 找到当前菜单相符合的所有子菜单
        const children = listToTree(list, item.menu_number)
        if (children.length) {
          item.children = children
        } else {
          item.children = []
        }
        // 加入到树中
        tree.push(item)
      }
    })
    return tree
  }
// 树形结构查找某一个
export const findNodeInTree = (tree, targetValue, found = null, str = 'pkey') => {
    if (tree[str] == targetValue) {
      return tree
    }
    for (let i = 0; i < tree.children.length; i++) {
      found = findNodeInTree(tree.children[i], targetValue, found, str)
      if (found) {
        return found
      }
    }
    return found
  }